package com.zfy.tingshu.search.service.impl;

import com.zfy.tingshu.album.client.AlbumInfoFeignClient;
import com.zfy.tingshu.album.client.CategoryFeignClient;
import com.zfy.tingshu.common.constant.RedisConstant;
import com.zfy.tingshu.common.result.Result;
import com.zfy.tingshu.model.album.AlbumInfo;
import com.zfy.tingshu.model.album.BaseCategoryView;
import com.zfy.tingshu.model.user.UserInfo;
import com.zfy.tingshu.search.service.ItemService;
import com.zfy.tingshu.user.client.UserInfoFeignClient;
import com.zfy.tingshu.vo.album.AlbumStatVo;
import com.zfy.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private CategoryFeignClient categoryFeignClient;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private RedissonClient redissonClient;
    @Override
    public Map<String, Object> getItem(Long albumId) {
        //创建map集合
        HashMap<String, Object> result = new HashMap<>();
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        if (!bloomFilter.contains(albumId)){
            log.error("专辑Id不存在{}",albumId);
            return result;
        }


        //多线程查询数据
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //远程调用
            Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(albumInfoResult, "查询专辑信息失败");
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "查询专辑信息失败");
            result.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolExecutor).whenCompleteAsync((v, t) -> {
            System.out.println("专辑标题：\t" + v.getAlbumTitle());
            if (t != null) {
                log.error("查询分类数据失败：{}", t.getMessage());
            }
        });
        //获取分类数据；
        CompletableFuture<Void> voidCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync((albumInfo) -> {
            Result<BaseCategoryView> basecategoryViewresult = categoryFeignClient.getBaseCategoryView(albumInfo.getCategory3Id());
            Assert.notNull("查询分类数据失败");
            BaseCategoryView baseCategoryView = basecategoryViewresult.getData();
            //存储数据
            result.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor).whenCompleteAsync((v, t) -> {
            if (t != null) {
                log.error("查询分类数据失败：{}" + t.getMessage());
            }
        });
        //获取用户Id
        CompletableFuture<Void> voidCompletableFuture1 = albumInfoCompletableFuture.thenAcceptAsync((albumInfo -> {
            Result<UserInfoVo> userInfoResult = userInfoFeignClient.getUserInfo(albumInfo.getUserId());
            Assert.notNull(userInfoResult, "查询用户信息失败");
            UserInfoVo userInfoVo = userInfoResult.getData();
            Assert.notNull(userInfoVo, "查询用户信息失败");
            //存储数据
            result.put("announcer", userInfoVo);
        }), threadPoolExecutor).whenCompleteAsync((v, t) -> {
            if (t != null) {
                log.error("查询用户信息失败：{}" + t.getMessage());
            }
        });
        //根据专辑Id  查询专辑统计数据
        CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.runAsync(() -> {
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.getAlbumStatVo(albumId);
            Assert.notNull(albumStatVoResult, "查询专辑统计数据失败");
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            Assert.notNull(albumStatVo, "查询专辑统计数据失败");
            result.put("albumStatVo", albumStatVo);
        }, threadPoolExecutor).whenCompleteAsync((v, t) -> {
            if (t != null) {
                log.error("查询统计数据失败：" + t.getMessage());
            }
        });
        //多任务组合
        CompletableFuture.allOf(albumInfoCompletableFuture,voidCompletableFuture,voidCompletableFuture1,voidCompletableFuture2).join();
        //返回数据
        return result;
    }
}
